#include "game.h"


//初始化棋盘
void InitBoard(char Board[ROWS][COLS],int rows,int cols,char set)
{
    for(int i = 0;i < rows;i++)
    {
        for(int j = 0;j < cols;j++)
        {
            Board[i][j] = set;
        }
    }
}

//打印棋盘
void DisplayBoard(char Board[ROWS][COLS],int row,int col)
{
    printf("------扫雷游戏------\n");
    for(int i = 0;i <= col;i++)//打印坐标
    {
        printf("%2d ",i);
    }
    printf("\n");

    for(int i = 1;i <= row;i++)
    {
        printf("%2d ",i);
        for(int j = 1;j <= col;j++)
        {
            printf("%2c ",Board[i][j]);
        }
        printf("\n");
    }
}

//布置雷
void SetMine(char board[ROWS][COLS],int row,int col)
{
    //布置10个雷
    //⽣成随机的坐标，布置雷
    int count = EASY_GAME;
    while(count)
    {
        int x = rand() % row + 1;//[1, row]
        int y = rand() % col + 1;
        if(board[x][y] == '0')
        {
            board[x][y] = '1';
            count--;
        }

    }

}


int GetMineCount(char mine[ROWS][COLS], int x, int y)
 {
        return (mine[x-1][y]+mine[x-1][y-1]+mine[x][y - 1]
        +mine[x+1][y-1]+mine[x+1][y]+mine[x+1][y+1]+mine[x][y+1]+mine[x-1][y+1] - 8 * '0');
 }
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
 {
    int x = 0;
    int y = 0;
    int win = 0;
    while (win < row * col- EASY_GAME)
    {
        printf("请输⼊要排查的坐标:>");
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (mine[x][y] == '1')
            {
                printf("很遗憾，你被炸死了\n");
                DisplayBoard(mine, ROW, COL);
                break;
            }
        else
            {
            //该位置不是雷，就统计这个坐标周围有⼏个雷
 
                int count = GetMineCount(mine, x, y);
                show[x][y] = count + '0';
                DisplayBoard(show, ROW, COL);
                win++;
            }
            }
        else
            {
                printf("坐标⾮法，重新输⼊\n");
            }
        }
        if (win == row * col - EASY_GAME)
        {
            printf("恭喜你，排雷成功\n");
                DisplayBoard(mine, ROW, COL);
        }
 }